<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册</title>
    <link rel="shortcut icon" href="../../favicon.ico" type="image/x-icon">
    <link rel="stylesheet" type="text/css" href="../../css/reset.css" />
    <link rel="stylesheet" type="text/css" href="../../css/register.css" />
</head>

<body>
    <div class="pos">
        <div class="login">
            <span class="close iconfont icon-close"></span>
            <img src="../../img/login/loginlogo.png" alt="">
            <ul>
                <li></li>
                <li class="phone">使用用户名登陆</li>
                <li></li>
            </ul>

            <input style="margin-top: 0;" type="text" id="username" placeholder="请输入您的用户名">

            <input type="password" id="password" placeholder="请输入密码">

            <input type="password" id="repwd" placeholder="请再次输入密码">
            <span class="tips"></span>
            <div class="clear"></div>
            <button class="loginbtn">注册</button>


            <p>
                <input type="checkbox" class="xuanze" />
                <span><a href="javascript:;">我同意 《用户使用协议》</a></span>
                <span>
                    已有账号
                    <a href="./login.html">去登陆</a>
                </span>
            </p>
        </div>
    </div>
    <!-- <script src="../../utils/utils.js"></script> -->
</body>
<script src="../../utils/jquery.min.js"></script>
<script src="../../utils/Http.js" type="module"></script>
<script type="module">
    import Http from '../../utils/Http.js'
    $('#username').blur(async function () {
        let username = $('#username').val();
        let reg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,15}$/;
        if (!reg.test(username)) {
            $('.tips').html('请输入6-15位数字和字母组合的用户名');
            return;
        } else {
            $('.tips').html('');
        }
        let {
            result
        } = await Http({
            url: '/api/register',
            type: 'post',
            data: {
                username,
            }
        })
        if (result.status == 200) {
            $('.tips').html('');
        }
        // else {
        //     $('.tips').html('有没有用户名');
        // }
    })

    $('#password').blur(async function () {
        let reg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,15}$/;
        let password = $('#password').val() //必须在里面拿 不然值一直为空
        let repwd = $('#repwd').val()
        //判断密码格式
        if (!reg.test(password)) {
            $('.tips').html('请输入6-15位数字和字母组合的密码');
            return;
        }
        //判断二次密码存在 并且 两次密码是否一致
        if (repwd != '' && password !== repwd) {
            $('.tips').html('两次密码不一致,请重新输入');
            return;
        }
        $('.tips').html('');
    })
    $('#repwd').blur(async function () {
        // let reg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,15}$/;
        let password = $('#password').val() //必须在里面拿 不然值一直为空
        let repwd = $('#repwd').val()
        //判断两次密码是否一致
        if (password !== repwd) {
            $('.tips').html('两次密码不一致,请重新输入');
            return;
        }
        $('.tips').html('');
    })
    $('.loginbtn').on('click', async function () {
        $('.tips').html('') //每次点击之后重置提示信息 

        let username = $('#username').val()
        let password = $('#password').val()
        let repwd = $('#repwd').val()
        //判断不能为空
        if (username == '' || password == '' || repwd == '') {
            $('.tips').html('用户名或密码不能为空');
            return;
        }
        if (password !== repwd) {
            $('.tips').html('两次密码不一致,请重新输入');
            $('#password').val('')
            $('#repwd').val('')
            return;
        }

        let reg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,15}$/;
        if (!reg.test(username)) {
            $('.tips').html('请输入6-15位数字和字母组合的用户名');
            return;
        } else {
            $('.tips').html('');
        }

        if (!reg.test(password)) {
            $('.tips').html('请输入6-15位数字和字母组合的密码');
            return;
        }

        let {
            result
        } = await Http({
            url: '/api/register',
            type: 'post',
            data: {
                username,
                password,
                repwd,
            }
        })
        if (result.status == 200) {
            alert('注册成功')
            location.href = './login.html'
        } else {
            $('.tips').html(result.msg);
            $('#username').val('')
        }
    })
</script>

</html>